home *** CD-ROM | disk | FTP | other *** search
Text File | 1994-04-21 | 10.8 KB | 311 lines | [TEXT/MPS ] |
- //========================================================================================
- //
- // File: FWClaInf.h
- // Release Version: $ 1.0d1 $
- //
- // Creation Date: 3/28/94
- //
- // Copyright: © 1994 by Apple Computer, Inc., all rights reserved.
- //
- //========================================================================================
-
- #ifndef FWCLAINF_H
- #define FWCLAINF_H
-
- #include <stddef.h>
-
- #ifndef FWSTDDEF_H
- #include "FWStdDef.h"
- #endif
-
- #ifdef FW_BUILD_WIN32S
- #include <Windows.h>
- #endif
-
- //========================================================================================
- // FW_CAN_EXPORT_DATA
- //========================================================================================
-
- #if defined(FW_BUILD_MAC) || defined(FW_BUILD_WIN16)
- #define FW_CAN_EXPORT_DATA
- #endif
-
- //========================================================================================
- // CLASS FW_CClassInfo
- //========================================================================================
-
- class FW_CClassInfo;
- typedef const FW_CClassInfo *FW_ClassReference;
-
- class FW_CClassInfo
- {
- public:
-
- ~FW_CClassInfo();
- FW_CClassInfo(const char *const className,
- size_t instanceSize,
- FW_ClassReference const *ancestors);
-
- const char *const GetClassName() const;
- // Returns a char string containing name of the class
-
- const size_t GetInstanceSize() const;
- // Returns the size of instances of the class
-
- FW_ClassReference const * GetAncestors() const;
- // Returns the list of ancestors of the class
-
- long GetHashKey() const;
- // Returns the hash key for the class name
-
- FW_Boolean IsBaseOf(FW_ClassReference aDerivedClass) const;
- // Return true if the class is or a base of the class represented by aDerivedClass
- // Note, usage is: this->IsBaseOf(aDerivedClass)
-
- FW_Boolean IsKindOf(FW_ClassReference aBaseClass) const;
- // Return true if the class is or inherits from class represented by aBaseClass
- // Note, usage is: this->IsKindOf(aBaseClass)
-
- int operator==(const FW_CClassInfo&) const;
- int operator!=(const FW_CClassInfo&) const;
-
- private:
-
- const char *const fClassName;
- // Name of the class
-
- size_t fInstanceSize;
- // Size of instances of the class
-
- FW_ClassReference const *fAncestors;
- // ClassInfo for ancestors of the class.
- // Pointer to array of const pointers to const FW_CClassInfo
-
- FW_CClassInfo(const FW_CClassInfo&);
- FW_CClassInfo& operator=(const FW_CClassInfo&);
- // ClassInfo objects cannot be copied
-
- };
-
- //----------------------------------------------------------------------------------------
- // FW_CClassInfo::operator==
- //----------------------------------------------------------------------------------------
-
- inline int FW_CClassInfo::operator==(const FW_CClassInfo& other) const
- {
- return &other == this;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CClassInfo::operator!=
- //----------------------------------------------------------------------------------------
-
- inline int FW_CClassInfo::operator!=(const FW_CClassInfo& other) const
- {
- return &other != this;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CClassInfo::GetClassName
- //----------------------------------------------------------------------------------------
-
- inline const char *const FW_CClassInfo::GetClassName() const
- {
- return fClassName;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CClassInfo::GetInstanceSize
- //----------------------------------------------------------------------------------------
-
- inline const size_t FW_CClassInfo::GetInstanceSize() const
- {
- return fInstanceSize;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CClassInfo::GetAncestors
- //----------------------------------------------------------------------------------------
-
- inline FW_ClassReference const * FW_CClassInfo::GetAncestors() const
- {
- return fAncestors;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CClassInfo::IsBaseOf
- //----------------------------------------------------------------------------------------
-
- inline FW_Boolean FW_CClassInfo::IsBaseOf(FW_ClassReference aDerivedClass) const
- {
- return aDerivedClass->IsKindOf(this);
- }
-
- //========================================================================================
- // Macros for generating and accessing class information
- //========================================================================================
-
- // ----- OPF internal macros.
- // Clients should use the public RTTI macros defined at the end of this file
-
- #ifdef FW_CAN_EXPORT_DATA
- #define FW_CLASS_REFERENCE(name) \
- (&name::gClassInfo)
- #else
- #define FW_CLASS_REFERENCE(name) \
- (&name::PrivStaticGetClassInfo())
- #endif
-
- #define _FW_CLASS_ANCESTORS(name) \
- name::gAncestors
-
- #ifdef FW_CAN_EXPORT_DATA
- #define _FW_DEFINE_CLASSINFO_OBJECT(name) \
- const FW_CClassInfo FW_SHARED_DATA \
- name::gClassInfo(#name, sizeof(name), _FW_CLASS_ANCESTORS(name));
- #else
- #define _FW_DEFINE_CLASSINFO_OBJECT(name) \
- const FW_CClassInfo FW_SHARED_DATA \
- name::gClassInfo(#name, sizeof(name), name::PrivGetAncestors()); \
- const FW_CClassInfo& name::PrivStaticGetClassInfo() { return name::gClassInfo; } \
- const FW_ClassReference* name::PrivGetAncestors() { return name::gAncestors; }
- #endif
-
- #define _FW_CLASS_ANCESTORS_IMPLEMENT_M0(name) \
- const FW_ClassReference FW_SHARED_DATA _FW_CLASS_ANCESTORS(name)[] = \
- { \
- 0 \
- };
-
- #define _FW_CLASS_ANCESTORS_IMPLEMENT_M1(name, ancestor) \
- const FW_ClassReference FW_SHARED_DATA _FW_CLASS_ANCESTORS(name)[] = \
- { \
- FW_CLASS_REFERENCE(ancestor), \
- 0 \
- };
-
- #define _FW_CLASS_ANCESTORS_IMPLEMENT_M2(name, ancestor1, ancestor2) \
- const FW_ClassReference FW_SHARED_DATA _FW_CLASS_ANCESTORS(name)[] = \
- { \
- FW_CLASS_REFERENCE(ancestor1), \
- FW_CLASS_REFERENCE(ancestor2), \
- 0 \
- };
-
- #define _FW_CLASS_ANCESTORS_IMPLEMENT_M3(name, ancestor1, ancestor2, ancestor3) \
- const FW_ClassReference FW_SHARED_DATA _FW_CLASS_ANCESTORS(name)[] = \
- { \
- FW_CLASS_REFERENCE(ancestor1), \
- FW_CLASS_REFERENCE(ancestor2), \
- FW_CLASS_REFERENCE(ancestor3), \
- 0 \
- };
-
- #define _FW_CLASS_ANCESTORS_IMPLEMENT_M4(name, ancestor1, ancestor2, ancestor3, ancestor4)\
- const FW_ClassReference FW_SHARED_DATA _FW_CLASS_ANCESTORS(name)[] = \
- { \
- FW_CLASS_REFERENCE(ancestor1), \
- FW_CLASS_REFERENCE(ancestor2), \
- FW_CLASS_REFERENCE(ancestor3), \
- FW_CLASS_REFERENCE(ancestor4), \
- 0 \
- };
-
- #define _FW_VIRTUAL_CLASSINFO_ACCESSOR(name) \
- FW_ClassReference name::PrivVirtualGetClassInfo(void) const \
- { \
- return FW_CLASS_REFERENCE(name); \
- }
-
- // ----- The public macros for declaring and defining RTTI scaffolding
-
- #ifdef FW_CAN_EXPORT_DATA
- #define FW_DECLARE_CLASS \
- virtual FW_ClassReference PrivVirtualGetClassInfo(void) const; \
- static const FW_CClassInfo FW_SHARED_DATA gClassInfo; \
- static const FW_ClassReference FW_SHARED_DATA gAncestors[];
- #else
- #define FW_DECLARE_CLASS \
- virtual FW_ClassReference PrivVirtualGetClassInfo(void) const; \
- static const FW_CClassInfo& PrivStaticGetClassInfo(); \
- static const FW_ClassReference* PrivGetAncestors(); \
- private: \
- static const FW_CClassInfo FW_SHARED_DATA gClassInfo; \
- static const FW_ClassReference FW_SHARED_DATA gAncestors[]; \
- public:
- #endif
-
- #define FW_DEFINE_CLASS_M0(name) \
- _FW_CLASS_ANCESTORS_IMPLEMENT_M0(name) \
- _FW_DEFINE_CLASSINFO_OBJECT(name) \
- _FW_VIRTUAL_CLASSINFO_ACCESSOR(name)
-
- #define FW_DEFINE_CLASS_M1(name, ancestor) \
- _FW_CLASS_ANCESTORS_IMPLEMENT_M1(name, ancestor) \
- _FW_DEFINE_CLASSINFO_OBJECT(name) \
- _FW_VIRTUAL_CLASSINFO_ACCESSOR(name)
-
- #define FW_DEFINE_CLASS_M2(name, ancestor1, ancestor2) \
- _FW_CLASS_ANCESTORS_IMPLEMENT_M2(name, ancestor1, ancestor2) \
- _FW_DEFINE_CLASSINFO_OBJECT(name) \
- _FW_VIRTUAL_CLASSINFO_ACCESSOR(name)
-
- #define FW_DEFINE_CLASS_M3(name, ancestor1, ancestor2, ancestor3) \
- _FW_CLASS_ANCESTORS_IMPLEMENT_M3(name, ancestor1, ancestor2, ancestor3) \
- _FW_DEFINE_CLASSINFO_OBJECT(name) \
- _FW_VIRTUAL_CLASSINFO_ACCESSOR(name)
-
- #define FW_DEFINE_CLASS_M4(name, ancestor1, ancestor2, ancestor3, ancestor4) \
- _FW_CLASS_ANCESTORS_IMPLEMENT_M4(name, ancestor1, ancestor2, ancestor3, ancestor4) \
- _FW_DEFINE_CLASSINFO_OBJECT(name) \
- _FW_VIRTUAL_CLASSINFO_ACCESSOR(name)
-
- //========================================================================================
- // RTTI Portability Macros
- //
- // These macros provide a transition path until all compilers support RTTI.
- //========================================================================================
-
- //----------------------------------------------------------------------------------------
- // Dynamic Cast Macro
- //----------------------------------------------------------------------------------------
-
- #ifdef FW_COMPILER_SUPPORTS_RTTI
- #define FW_DYNAMIC_CAST(T, p) dynamic_cast<T*>(p)
- #else
- #define FW_DYNAMIC_CAST(T, p) \
- ( (p) && (p)->PrivVirtualGetClassInfo()->IsKindOf(FW_CLASS_REFERENCE(T)) ? (T*)p : (T*)0 )
- #endif
-
- //----------------------------------------------------------------------------------------
- // typeid macros
- //----------------------------------------------------------------------------------------
-
- // Note, C++ typeid's are TypeInfo *references*.
- // OPF TYPEIDs are ClassInfo *references*
- // FW_ClassReference are ClassInfo *pointers*.
- // Yes, it's ugly, but fix it later. ???JEL
-
- #ifdef FW_COMPILER_SUPPORTS_RTTI
- #define FW_TYPEID_FROM_TYPE(type) (typeid(type))
- #define FW_TYPEID_FROM_POINTER(p) (typeid(*(p)))
- #else
- #define FW_TYPEID_FROM_TYPE(type) (*FW_CLASS_REFERENCE(type))
- #define FW_TYPEID_FROM_POINTER(p) (*(p)->PrivVirtualGetClassInfo())
- #endif
-
- //----------------------------------------------------------------------------------------
- // Class name macros
- //----------------------------------------------------------------------------------------
-
- #ifdef FW_COMPILER_SUPPORTS_RTTI
- #define FW_CLASSNAME_FROM_TYPE(type) (typeid(type).name())
- #define FW_CLASSNAME_FROM_POINTER(p) (typeid(*(p)).name())
- #else
- #define FW_CLASSNAME_FROM_TYPE(type) (FW_TYPEID_FROM_TYPE(type).GetClassName())
- #define FW_CLASSNAME_FROM_POINTER(p) (FW_TYPEID_FROM_POINTER(p).GetClassName())
- #endif
-
- #endif
-
-